/*
 * Copyright (C) 2012-2013 Michael L.R. Marques
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact: michaellrmarques@gmail.com
 */
package com.jm.jmsql.objects.events;

import com.jm.commons.utils.Constants;
import com.jm.jmsql.objects.Definition;
import java.util.EventObject;

/**
 *
 * @created Feb 19, 2013
 * @author Michael L.R. Marques
 */
public class DefinitionEvent extends EventObject {
    
    /**
     * The Event type
     */
    private DefinitionEvent.EventType type;
    
    /**
     * The SQL Query that was executed
     */
    private String sql;
    
    /**
     * The starting system milliseconds
     */
    private long start;
    
    /**
     * The ending system milliseconds
     */
    private long end;
    
    /**
     * 
     */
    private boolean result;
    
    /**
     * Set by the client to cancel the operation
     */
    private boolean cancel;
    
    /**
     * 
     * @param definition
     * @param type 
     */
    public DefinitionEvent(Definition definition, DefinitionEvent.EventType type) {
        this(definition, type, 0);
    }
    
    /**
     * 
     * @param definition
     * @param type
     * @param start 
     */
    public DefinitionEvent(Definition definition, DefinitionEvent.EventType type, long start) {
        this(definition, type, start, start);
    }
    
    /**
     * 
     * @param definition
     * @param type
     * @param sql
     * @param start 
     */
    public DefinitionEvent(Definition definition, DefinitionEvent.EventType type, String sql, long start) {
        this(definition, type, sql, start, start, false);
    }
    
    /**
     * 
     * @param definition
     * @param type
     * @param start
     * @param end 
     */
    public DefinitionEvent(Definition definition, DefinitionEvent.EventType type, long start, long end) {
        this(definition, type, Constants.EMPTY_STRING, start, end, false);
    }
    
    /**
     * 
     * @param definition
     * @param type
     * @param sql
     * @param start
     * @param end 
     * @param result 
     */
    public DefinitionEvent(Definition definition, DefinitionEvent.EventType type, String sql, long start, long end, boolean result) {
        super(definition);
        this.type = type;
        this.sql = sql;
        this.start = start;
        this.end = end;
        this.result = result;
    }
    
    /**
     * 
     * @return 
     */
    public Definition getDefinition() {
        return (Definition) this.source;
    }
    
    /**
     * 
     * @return 
     */
    public DefinitionEvent.EventType getEventType() {
        return this.type;
    }
    
    /**
     * 
     * @return 
     */
    public String getSQLQuery() {
        return this.sql;
    }
    
    /**
     * 
     * @return 
     */
    public long getStart() {
        return this.start;
    }
    
    /**
     * 
     * @return 
     */
    public long getEnd() {
        return this.end;
    }
    
    /**
     * 
     * @return 
     */
    public long getSpent() {
        return this.end - this.start;
    }
    
    /**
     * 
     * @return 
     */
    public boolean getHasResults() {
        return this.result;
    }
    
    /**
     * 
     * @param cancel 
     */
    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }
    
    /**
     * 
     * @return 
     */
    public boolean isCancelled() {
        return this.cancel;
    }
    
    /**
     * Defines the event types
     */
    public static final class EventType {
        
        /**
         * 
         */
        private String typeString;
        
        /**
         * 
         * @param s 
         */
        private EventType(String s) {
            this.typeString = s;
        }

        /**
         * Connecting type.
         */
        public static final DefinitionEvent.EventType CONNECTING = new DefinitionEvent.EventType("CONNECTING");

        /**
         * Connected type.
         */
        public static final DefinitionEvent.EventType CONNECTED = new DefinitionEvent.EventType("CONNECTED");
        
        /**
         * Connected type.
         */
        public static final DefinitionEvent.EventType CONNECTION_SUCCESSFUL = new DefinitionEvent.EventType("CONNECTION_SUCCESSFUL");
        
        /**
         * Connected type.
         */
        public static final DefinitionEvent.EventType CONNECTION_FAILED = new DefinitionEvent.EventType("CONNECTION_FAILED");

        /**
         * Disconnecting type.
         */
        public static final DefinitionEvent.EventType DISCONNECTING = new DefinitionEvent.EventType("DISCONNECTING");
        
        /**
         * Disconnected Type.
         */
        public static final DefinitionEvent.EventType DISCONNECTED = new DefinitionEvent.EventType("DISCONECTED");
        
        /**
         * Executing Type.
         */
        public static final DefinitionEvent.EventType EXECUTING = new DefinitionEvent.EventType("EXECUTING");
        
        /**
         * Executed Type
         */
        public static final DefinitionEvent.EventType EXECUTED = new DefinitionEvent.EventType("EXECUTED");

        /**
         * Converts the type to a string.
         *
         * @return the string
         */
        @Override public String toString() {
            return this.typeString;
        }
        
    }

}
